package com.huawei.gzq.controller;

import com.huawei.gzq.domain.User;
import com.huawei.gzq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;


    @RequestMapping(value = "/find", method = RequestMethod.GET)
    public String user(@RequestParam int id) {
        System.out.println("id: " + id);
        User user = userService.findUser(id);
        return user.toString();
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public String all() {
        List<User> userList = userService.findAll();
        String text = "";
        for (User user : userList)
            text += user.toString() + "<br>";
        return text;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addUser(@RequestParam int id) {
        User newUser = User.builder().id(id).email(id + "xxx@xxx.com").build();
        return userService.addUser(newUser);
    }
}
